package com.jn.erp.web.api.student.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jn.erp.web.api.student.entity.TRenewalAlertsConfig;
import com.jn.erp.web.api.student.entity.dto.StudentRenewalDto;
import com.jn.erp.web.api.student.mapper.RenewalAlertsConfigMapper;
import com.jn.erp.web.api.student.service.RenewalAlertsConfigService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

import static com.jn.erp.security.utils.SecurityUtils.getUserId;

@Service
public class RenewalAlertsServiceImpl extends ServiceImpl<RenewalAlertsConfigMapper, TRenewalAlertsConfig> implements RenewalAlertsConfigService {

    @Autowired
    private RenewalAlertsConfigMapper renewalAlertsMapper;


    @Override
    public void alertSettings(StudentRenewalDto studentRenewalDto) {
        Integer id = studentRenewalDto.getId();
        TRenewalAlertsConfig tRenewalAlertsConfig1 = renewalAlertsMapper.selectById(id);
        if (tRenewalAlertsConfig1 != null) {
            if (tRenewalAlertsConfig1.getOrgId() == studentRenewalDto.getOrgId()) {
                tRenewalAlertsConfig1.setRemainingClass(studentRenewalDto.getRemainingClass());
                tRenewalAlertsConfig1.setRemainingDay(studentRenewalDto.getRemainingDay());
                tRenewalAlertsConfig1.setIsDelete("0");
                tRenewalAlertsConfig1.setUpdateUser(String.valueOf(getUserId()));
                tRenewalAlertsConfig1.setCreateDate(LocalDateTime.now());
                tRenewalAlertsConfig1.setType(studentRenewalDto.getType());
                renewalAlertsMapper.updateById(tRenewalAlertsConfig1);
            }
        }else {
                tRenewalAlertsConfig1.setRemainingClass(studentRenewalDto.getRemainingClass());
                tRenewalAlertsConfig1.setRemainingDay(studentRenewalDto.getRemainingDay());
            tRenewalAlertsConfig1.setType(studentRenewalDto.getType());
                tRenewalAlertsConfig1.setOrgId(studentRenewalDto.getOrgId());
                renewalAlertsMapper.insert(tRenewalAlertsConfig1);
            }
        }

            @Override
            public TRenewalAlertsConfig settingsList (TRenewalAlertsConfig tRenewalAlertsConfig){
                QueryWrapper<TRenewalAlertsConfig> qw = new QueryWrapper<>();
                qw.eq("org_id", tRenewalAlertsConfig.getOrgId());
                qw.eq("is_delete", 0);
                TRenewalAlertsConfig Config = renewalAlertsMapper.selectOne(qw);
                return Config;
            }
        }
